﻿using Blog.Application.Condition;
using Blog.Application.DTO;
using Blog.Domain;
using Blog.Repository;
using Core.Cache;
using Core.Common;
using Core.CPlatform;
using Core.EventBus;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Blog.Application.Service.imp
{
    public class FriendLinkService : IFriendLinkService
    {
        private IFriendLinkRepository _friendLinkRepository;
        private ICacheFactory _cacheFactory;
        private IEventBus _eventBus;
        public FriendLinkService(IFriendLinkRepository friendLinkRepository, ICacheFactory cacheFactory, IEventBus eventBus)
        {
            _friendLinkRepository = friendLinkRepository;
            _cacheFactory = cacheFactory;
            _eventBus = eventBus;
        }
        public List<FriendLinkDTO> GetFriendLinks()
        {
            ICacheClient cacheClient= _cacheFactory.CreateClient(CacheType.Redis);
            string cacheValue= cacheClient.StringGet(CacheKey.FRIEND_LINK_KEY);
            if (!string.IsNullOrEmpty(cacheValue))
                return JsonConvert.DeserializeObject<List<FriendLinkDTO>>(cacheValue);
            FriendLinkCodnition friendLinkCodnition = new FriendLinkCodnition();
            friendLinkCodnition.IsPass = true;
            Expression<Func<FriendLink, bool>> expression = friendLinkCodnition.BuildExpression();
            IEnumerable<FriendLink> friendLinks = _friendLinkRepository.Select(expression);
            List<FriendLinkDTO> list = new List<FriendLinkDTO>();
            foreach(var item in friendLinks)
            {
                FriendLinkDTO friendLinkDTO = new FriendLinkDTO();
                friendLinkDTO.Img = item.Photo;
                friendLinkDTO.Link = item.Address;
                friendLinkDTO.WebName = item.Name;
                friendLinkDTO.Email = item.Email;
                list.Add(friendLinkDTO);
            }
            cacheClient.StringSet(CacheKey.FRIEND_LINK_KEY, list);
            return list;
        }

        public void Apply(FriendLinkDTO friendLinkDTO)
        {
            if (string.IsNullOrEmpty(friendLinkDTO.WebName))
                throw new ValidationException("站点名称为空");
            if (string.IsNullOrEmpty(friendLinkDTO.Link))
                throw new ValidationException("站点地址为空");
            if (string.IsNullOrEmpty(friendLinkDTO.Img))
                throw new ValidationException("站点图标为空");
            FriendLink friendLink = new FriendLink();
            friendLink.Email = friendLinkDTO.Email;
            friendLink.Address = friendLinkDTO.Link;
            friendLink.IsPass = false;
            friendLink.Photo = friendLinkDTO.Img;
            friendLink.Name = friendLinkDTO.WebName;
            friendLink.CreateTime = DateTime.Now;
            _friendLinkRepository.Insert(friendLink);
            EmailData emailData = new EmailData();
            emailData.Body = string.Format("《{0}：{1}》申请添加友链",friendLink.Name,friendLink.Address);
            emailData.Subject = "天天博客有个新留言";
            _eventBus.Publish(emailData);
        }
    }
}
